/**
 * 
 */
package fi.foyt.cs.api.users;

import org.json.JSONException;
import org.json.JSONObject;
import org.restlet.data.Reference;
import org.restlet.data.Status;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.ext.oauth.protectedresource.ScopedResource;
import org.restlet.resource.Get;
import org.restlet.resource.ResourceException;

import fi.foyt.cs.Scope;
import fi.foyt.cs.api.APIResource;
import fi.foyt.cs.controller.UserController;
import fi.foyt.cs.persistence.domainmodel.user.User;

public class MeResource extends APIResource implements ScopedResource {

  @Get("json")
  public JsonRepresentation represent() throws ResourceException {
    UserController userController = new UserController();
    
    JSONObject body = new JSONObject();
    try {
      User user = userController.findUserByIdentifier(getClientInfo().getUser().getIdentifier());
      body.put("id", user.getId().getId());
      body.put("email", user.getEmail());
      body.put("firstName", user.getLastName());
      body.put("lastName", user.getFirstName());
    } catch (JSONException e) {
      this.setStatus(Status.SERVER_ERROR_INTERNAL);
      return null;
    }
    return new JsonRepresentation(body);
  }

  public String getOwner(Reference uri) {
    return null;
  }

  public String[] getScope(Reference uri) {
    return new String[] { Scope.BasicInfo.getIntenal() };
  }
}
